package com.crm.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crm.model.BizProduct;
import com.crm.service.BizProductService;
import com.crm.util.JsonUtil;
import com.crm.util.ResponseUtil;
import com.crm.util.ValidateUtil;
import com.github.pagehelper.Page;

@Controller
@RequestMapping("/bizProduct")
public class BizProductController {

	@Autowired
	private BizProductService bizProductService;
	
	@RequestMapping("/index")
	public String index(){
		
		return "configuration/productConfig";
	}
	
	@RequestMapping("/getAll")
	@ResponseBody
	public void getAll(HttpServletResponse response,HttpServletRequest request, Page<BizProduct> page, BizProduct record){
		
		Page<BizProduct> list = null;
		
		if(record.getProjectType() == 1)
			list = (Page<BizProduct>) bizProductService.selectAllByProfessional(page, record);
		else if(record.getProjectType() == 2)
			list = (Page<BizProduct>) bizProductService.selectAllByEducation(page, record);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/getAllPrice")
	@ResponseBody
	public void getAllPrice(HttpServletResponse response,HttpServletRequest request, Page<BizProduct> page, BizProduct record){
		Page<BizProduct> list = null;
		
		if(record.getProjectType() == 1)
			list = (Page<BizProduct>) bizProductService.selectAllByProfessionalPrice(page, record);
		else if(record.getProjectType() == 2)
			list = (Page<BizProduct>) bizProductService.selectAllByEducationPrice(page, record);
		
		ResponseUtil.resultPage(response,request,list);
	}
	
	@RequestMapping("/addNewRecord")
	@ResponseBody
	public Map<String, Object> addNewRecord(BizProduct record, String[] brand, String[] school){
		
		Map<String, Object> map = new HashMap<>();
		
		if(ValidateUtil.isEmpty(record.getClassName()))
			throw new RuntimeException("名称不能为空！");
		
		if(ValidateUtil.isEmpty(record.getProjectId()))
			throw new RuntimeException("所属项目不能为空！");
		
		if(!ValidateUtil.isEmpty(record.getProductId())){
			bizProductService.updateByPrimaryKey(record);
		}else{
			bizProductService.insert(record);
		}
		
		map.put("status", "success");
		map.put("msg", "新增成功！");
		
		return map;
	}
	
	@RequestMapping("/deleteRecord")
	@ResponseBody
	public Map<String, Object> deleteRecord(BizProduct record){
		
		Map<String, Object> map = new HashMap<>();
		
		record.setDeleteMark(0);
		bizProductService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "删除成功！");
		
		return map;
	}
	
	@RequestMapping("/updateRecord")
	@ResponseBody
	public Map<String, Object> updateRecord(BizProduct record){
		
		Map<String, Object> map = new HashMap<>();
		
		bizProductService.updateByPrimaryKey(record);
		
		map.put("status", "success");
		map.put("msg", "修改成功！");
		
		return map;
	}
	
	/**
	 * 
	 * @Description : 加载班型数据
	 * @author zealot
	 * @param @param response
	 * @param @param record
	 * @return void
	 * @version 创建时间 : 2016年11月25日 下午7:49:27
	 * @thorws
	 */
	@RequestMapping("/loadProduct")
	public void loadProduct(HttpServletResponse response,BizProduct record){
		List<BizProduct> list = bizProductService.selectByUs(record);
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
		
	}
	
	/**
	 * 
	 * @Description : 加载产品价格
	 * @author zealot
	 * @param @param response
	 * @param @param record
	 * @return void
	 * @version 创建时间 : 2016年11月30日 上午10:51:02
	 * @thorws
	 */
	@RequestMapping("/loadProductPrice")
	public void loadProductPrice(HttpServletResponse response,BizProduct record){
		List<BizProduct> list = bizProductService.selectByUsPrice(record);
		ResponseUtil.result(response, JsonUtil.toJsonStr(list));
	}
	
	/**
	 * 
	 * @Description : 获取院校
	 * @author zealot
	 * @param @param response
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2016年12月27日 上午10:02:58
	 * @thorws
	 */
	@RequestMapping("/ajaxLoadSchool")
	@ResponseBody
	public void ajaxLoadSchool(HttpServletResponse response,BizProduct t){
		ResponseUtil.result(response, JsonUtil.toJsonStr(bizProductService.selectByUsForSchool(t)));
	}
	
	/**
	 * 
	 * @Description : 加载专业
	 * @author zealot
	 * @param @param response
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2017年1月4日 上午11:47:12
	 * @thorws
	 */
	@RequestMapping("/ajaxLoadMajor")
	@ResponseBody
	public void ajaxLoadMajor(HttpServletResponse response,BizProduct t){
		ResponseUtil.result(response, JsonUtil.toJsonStr(bizProductService.selectByUsForMajor(t)));
	}
	
	/**
	 * 
	 * @Description : 加载班型
	 * @author zealot
	 * @param @param response
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2017年1月4日 下午1:34:54
	 * @thorws
	 */
	@RequestMapping("/ajaxLoadClass")
	@ResponseBody
	public void ajaxLoadClass(HttpServletResponse response,BizProduct t){
		ResponseUtil.result(response, JsonUtil.toJsonStr(bizProductService.selectByUsForClass(t)));
	}
	
	/**
	 * 
	 * @Description : 加载班型及其价格
	 * @author zealot
	 * @param @param response
	 * @param @param t
	 * @return void
	 * @version 创建时间 : 2017年1月4日 下午1:42:57
	 * @thorws
	 */
	@RequestMapping("/ajaxLoadPirce")
	@ResponseBody
	public void ajaxLoadPirce(HttpServletResponse response,BizProduct t){
		ResponseUtil.result(response, JsonUtil.toJsonStr(bizProductService.selectByUsForPrice(t)));
	}
	
}
